﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain;
using Zocono.WMS.Infrastructure.BaseRepository.SqlSugarUtil;

namespace WcsTestProject
{
    public class WcsTestBase
    {
        private IServiceProvider serviceProvider;
        private IServiceCollection seveceCollection;

        public WcsTestBase()
        {
            seveceCollection = new ServiceCollection();
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");
            IConfiguration Configuration = builder.Build();
            seveceCollection.AddSingleton<IConfiguration>(Configuration);

            var sqlSugarConfig = SqlSugarConfig.GetConnectionString(Configuration);//连接数据库
            seveceCollection.AddSqlSugarClient<SqlSugar.SqlSugarClient>(config =>
            {
                config.ConnectionString = sqlSugarConfig.Item2;
                config.DbType = sqlSugarConfig.Item1;
                config.IsAutoCloseConnection = true;
                config.InitKeyType = InitKeyType.Attribute;
                //config.IsShardSameThread = true;
            });

            seveceCollection.AddNlog();
            seveceCollection.AddSingleton<ISnowflake, Snowflake>();
            seveceCollection.AddAutoMapper(typeof(AutoMapperConfigProfile));
        }

        public IServiceProvider ServiceProvider
        {
            get
            {
                if (serviceProvider == null)
                {
                    serviceProvider = seveceCollection.BuildServiceProvider();
                }
                return serviceProvider;
            }
        }
        public IServiceCollection ServiceCollection
        {
            get
            {
                return seveceCollection;
            }
        }
    }
}
